package com.yixiu.javabase.modules.dynamic.instrumentation.demo1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

class Transformer implements ClassFileTransformer {

    // 注意：必须是编译后的文件
    public static final String classNumberReturns2 = "E:\\Git\\Java\\JavaLearning\\src\\main\\java\\com\\yixiu\\javabase\\modules\\dynamic\\instrumentation\\demo1\\classes\\TransClass2.java.2";

    public static byte[] getBytesFromFile(String fileName) {
        try {
            // precondition
            File file = new File(fileName);
            InputStream is = new FileInputStream(file);
            long length = file.length();
            byte[] bytes = new byte[(int) length];

            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset <bytes.length
                    && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }

            if (offset < bytes.length) {
                throw new IOException("Could not completely read file "
                        + file.getName());
            }
            is.close();

            System.out.println("class length:" + String.valueOf( bytes.length ) );

            return bytes;
        } catch (Exception e) {
            System.out.println("error occurs in _ClassTransformer!"
                    + e.getClass().getName());
            return null;
        }
    }

    public byte[] transform(ClassLoader l, String className, Class<?> c,
                            ProtectionDomain pd, byte[] b) throws IllegalClassFormatException {


        System.out.println("load:" + className );

        //  load:com/yixiu/javabase/modules/dynamic/instrumentation/demo1/TransClass
        if(!className.contains("TransClass") ) {
            return null;
        }
//        if (!className.equals("TransClass")) {
//            return null;
//        }
        return getBytesFromFile(classNumberReturns2);

    }
}